package com.fastloan.jn.mgt.process.tactics.impl;

import com.fastloan.jn.mgt.process.core.ReturnData;
import com.fastloan.jn.mgt.process.model.JnAppApproveUser;
import com.fastloan.jn.mgt.process.model.JnApplicationApprovalReview;
import com.fastloan.jn.mgt.process.service.AppApproveService;
import com.fastloan.jn.mgt.process.service.AppApproveUserService;
import com.fastloan.jn.mgt.process.service.ApplicationApprovalReviewService;
import com.fastloan.jn.mgt.process.service.ProcessService;
import com.fastloan.jn.mgt.process.tactics.TacticsGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import static com.fastloan.jn.mgt.application.basic.enums.ApplicationStatus.APP_REJECT;
import static com.fastloan.jn.mgt.application.basic.enums.ApplicationStatus.RETURN_MANAGER;
import static com.fastloan.jn.mgt.application.basic.enums.ApplicationStatus.UNDER_REVIEW;
import static com.fastloan.jn.mgt.process.enums.AppApproveStatus.*;
import static com.fastloan.jn.mgt.process.enums.TacticsType.APPROVE;


/**
 * 审贷会审批流程
 * Created by chenkai on 2017/3/4.
 */
@Service
public class ApprovalFlow implements TacticsGenerator {
    @Autowired
    private AppApproveUserService appApproveUserService;

    @Autowired
    private ProcessService processService;

    @Autowired
    private AppApproveService appApproveService;

//    @Autowired
//    private RiskCustomerService riskCustomerService;

    /**
     * 审贷会
     */
    @Autowired
    private ApplicationApprovalReviewService tApplicationApprovalReviewService;


    @Override
    @Transactional
    public String runTactics(JnAppApproveUser appApproveUser, String applicationId) {
        if (ifAdopt(appApproveUser, applicationId).isPass()) {
            JnApplicationApprovalReview approvalReview = tApplicationApprovalReviewService.selectByPrimaryKey(appApproveUser.getApprovalReviewId());
            String approvalId = approvalReview.getApprovalId();
         //   OperationLogUtil.addLog(APP_APPROVE, LOAN_APPROVAL, SUCCESS);
            return approvalId;
        }
   //     OperationLogUtil.addLog(APP_APPROVE, LOAN_APPROVAL, FAIL);
        return null;
    }

    @Override
    public Integer getTacticsType() {
        return APPROVE.getValue();
    }

    /**
     * 判断审贷会成员审批信息
     *
     * @param appApproveUser
     * @param applicationId
     * @return
     */
    private ReturnData ifAdopt(JnAppApproveUser appApproveUser, String applicationId) {
        String approvalReviewId = appApproveUser.getApprovalReviewId();
        appApproveUserService.updateByPrimaryKeySelective(appApproveUser);
        Boolean examineAdopt = examineAdopt(approvalReviewId);//判断审贷会成员是否通过审批
        JnApplicationApprovalReview review = tApplicationApprovalReviewService.selectByPrimaryKey(approvalReviewId);
        if (examineAdopt) {
            review.setReviewStatus(OFF_THE_STOCKS.getValue());
            tApplicationApprovalReviewService.updateByPrimaryKeySelective(review);
            return new ReturnData(Boolean.TRUE);
        } else {
            processService.updateAppApprove(review.getApprovalId());
        }
        return new ReturnData(Boolean.FALSE);
    }

    /**
     * 更新审贷会成员信息
     *
     * @param approvalReviewId
     * @return
     */
    private Boolean examineAdopt(String approvalReviewId) {
        Example example = new Example(JnAppApproveUser.class);
        example.createCriteria().andEqualTo("approvalReviewId", approvalReviewId);
        return appApproveUserService.selectByExample(example).stream()
                .filter(appApproveUser -> null != appApproveUser.getApprovalConclution())
                .anyMatch(appApproveUser -> {
                    JnAppApproveUser approveUser = new JnAppApproveUser();
                    approveUser.setApprovalReviewId(approvalReviewId);
                    if (appApproveUser.getApprovalConclution().equals(USER_APPROVALCONCLUTION_OTHER_REJECT.getValue())) {  //拒绝
                    //    riskCustomerService.insertRiskCustomer(applicationId);
                        approveUser.setStatus(STATUS_OTHER_REJECT.getValue());
                        appApproveService.insertApproval(appApproveUser, APP_REJECT);
                    } else if (appApproveUser.getApprovalConclution().equals(USER_APPROVALCONCLUTION_BACK.getValue())) { //退回客户经理
                        approveUser.setStatus(STATUS_OTHER_UNTREAD.getValue());
                        appApproveService.insertApproval(appApproveUser, RETURN_MANAGER);
                    } else {                                                        //通过
                        approveUser.setStatus(STATUS_ADOPT.getValue());
                        appApproveUserService.updateStatus(approveUser);
                        appApproveService.insertApproval(appApproveUser, UNDER_REVIEW);
                        return true;
                    }
                    appApproveUserService.updateStatus(approveUser);
                    return false;
                });
    }

}
